package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 中国消费网
 * @author Persh
 *
 */
public class CcnNewsAnalyse implements AnalyseNews {
//http://www.ccn.com.cn/news/yaowen/2014/0110/526353.html
	private static final String[] regex={
		"http://www.ccn.com.cn/[0-9]*/[0-9]*.html",
//		"http://news.ccn.com.cn/[0-9]+.html",
//		"http://[0-9a-z]+.ccn.com.cn/\\?p=[0-9]+"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("div.title").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("div.fl h2").text();
			if(StringUtils.isBlank(title)){
				title =doc.select("h3.entry-title").text();
			}
		}
		if(StringUtils.isBlank(title)){
			title=doc.select(".zx_wzbtitle").text();
		}
		
		author=JSoupUtils.matchAuthor(doc,"来源：","作者：");

		date=DateUtils.matchDate(doc.select("div.info").text());
		if(date == null){
			date=DateUtils.matchDate(doc.select("div p").text());
			if(date == null){
				date=DateUtils.matchDate(doc.select("div.meta-info span").text());
				if(date == null){
					date= JSoupUtils.matchDate(doc, "时间:");
					if(date == null){
						date=DateUtils.matchDate(doc.select("div.meta-info span[style=float:left;]").text());
					}
				}

			}
		}
	
		doc.select("p:containsOwn(上一篇：) ~ *").remove();
		doc.select("p:containsOwn(上一篇：)").remove();
		content=HtmlCleaner.getContentHtml(url ,doc.select("div.zx_wzp"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url ,doc.select("div.con"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url ,doc.select("div.td-post-text-content"));
			}

		}


		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		CcnNewsAnalyse analyse=new CcnNewsAnalyse();
		String url="http://www.ccn.com.cn/318/902730.html";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
